<?php

namespace app\api\controller\v46;

use app\aliyun\controller\AliyunVOD;
use app\api\controller\v13\Auth2;
use app\zyhsapi\model\good_rtc_live\ZyhsGoodRtcLiveMedia;
use app\zyhsapi\model\good_rtc_live\ZyhsGoodRtcLiveNoMsgRecord;
use app\zyhsapi\model\good_rtc_live\ZyhsGoodRtcLiveRecord;
use app\zyhsapi\model\ZyhsConfig;
use think\response\Json;

class GoodRtcLive extends Auth2
{
    /**
     * 获取直播间信息
     * @return Json
     * @throws \Exception
     */
    public function getLiveRoomInfo()
    {
        $zyhsGoodRtcLiveRecordModel = new ZyhsGoodRtcLiveRecord();
        $liveRecordId = $zyhsGoodRtcLiveRecordModel->getNewestRecordId(2);

        if ($this->user)
        {
            $testAccountConfig = (new ZyhsConfig())->configInfo('rtc');
            $adminUserIds = $testAccountConfig['live_admin_uids'] ?? '';
            $adminUserIds = $adminUserIds ? explode(',', $adminUserIds) : [];
            $isAdmin = in_array($this->user['id'], $adminUserIds) ? 1 : 0;
        } else
        {
            $isAdmin = 0;
        }
        $status = 0;
        $onlineNum = 0;
        $sendMsgRecord = [];
        $audioInfo = null;
        if ($liveRecordId)
        {   //  直播中 直播记录详情
            $recordInfo = $zyhsGoodRtcLiveRecordModel->getInfoById($liveRecordId);
            $status = $recordInfo['status'] === 1 ? 1 : 0;
            $onlineNum = $zyhsGoodRtcLiveRecordModel->getOnlineNum($liveRecordId);
            $onlineNum = $onlineNum + $recordInfo['base_num'];
            // 获取 播放音频信息
            $audioInfo = $zyhsGoodRtcLiveRecordModel->getPlayAudioInfo($recordInfo, 0, 2);
            $audioInfo['sts_sign'] = (new AliyunVOD())->getStsToken($this->headers['app-type'], 3600);
            //  未登录 或者 未禁言 获取 历史弹幕消息
            if (!$this->user || !(new ZyhsGoodRtcLiveNoMsgRecord())->getDisenableSpeakStatus(2, $this->user['id']))
            {   // 发送 历史弹幕消息 列表
                $sendMsgRecord = $zyhsGoodRtcLiveRecordModel->getHistoryMsgData($liveRecordId);
            }
        }
        $resData = [
            'avatar' => '/zyhsapp/good_rtc/live_default_avatar1.png',
            'nickname' => '官方推荐',
            'online_num' => '在线：' . $onlineNum,
            'status' => $status,
            'id' => $liveRecordId,
            'msg_info' => [
                'color' => '#0DE1EB',
                'content' => '通知：本房间禁止传播封建迷信，禁止散播不利于社会和谐和团结的言论，禁止辱骂他人，请用户遵守行为准则。',
            ],
            'send_msg_record' => $sendMsgRecord,
            'is_admin' => $isAdmin,
            'audio_info' => $audioInfo,
            'default_bg' => '/zyhsapp/good_rtc/good_rtc_live_bg.json', // 背景图 lottle
            'audio_desc' => '优质连线内容循播 内容已经过用户同意公开 严格保护隐私',
            'uen' => $this->user['uen'] ?? '',
        ];

        return show(config('code.success'), '请求成功', $resData);
    }

    /**
     * 获取直播播放音频资源信息
     * @return Json
     * @throws \Exception
     */
    public function getLivePlayMediaInfo()
    {
        $liveRecordId = input('live_record_id/d', 0);
        $mid = input('id/d', 0);

        $zyhsGoodRtcLiveRecordModel = new ZyhsGoodRtcLiveRecord();
        $recordInfo = $zyhsGoodRtcLiveRecordModel->getInfoById($liveRecordId);

        if (!$recordInfo || !isset($recordInfo['status']) || $recordInfo['status'] !== 1)
        {
            return show(config('code.success'), '请求成功', [
                'status' => 0,
                'audio' => '',
                'duration' => 0,
                'start_duration' => 0,
                'live_avatar' => '',
                'live_name' => '',
                'user_avatar' => '',
                'user_name' => '',
                'astr_id' => 0,
                'bg_img' => '',
                'id' => 0,
            ]);
        }

        if ($mid && $mid === $recordInfo['current_mid'])
        {   // 获取下一个音频信息
            $zyhsGoodRtcLiveMediaModel = new ZyhsGoodRtcLiveMedia();
            $mediaList = $zyhsGoodRtcLiveMediaModel->where(['status' => 1])->order('sort DESC,id asc')->column('id');
            $i = array_search($mid, $mediaList);
            $i++;
            if ($i >= count($mediaList))
            {
                $i = 0;
            }
            $mid = $mediaList[$i];
        }

        $audioInfo = $zyhsGoodRtcLiveRecordModel->getPlayAudioInfo($recordInfo, $mid, 2);
        $audioInfo['status'] = 1;
        // 只有 可播放的章节 信息 才获取播放签名信息    1小时 有效期
        $audioInfo['sts_sign'] = (new AliyunVOD())->getStsToken($this->headers['app-type'], 3600);
        return show(config('code.success'), '请求成功', $audioInfo);
    }

    /**
     * 获取 举报原因
     * @return Json
     */
    public function getReportReason()
    {
        $data = [
            '色情低俗',
            '线下导流',
            '违法违规',
            '涉嫌欺诈',
            '广告刷屏',
            '言语辱骂',
            '其他',
        ];

        return show(config('code.success'), '请求成功', $data);
    }
}
